package com.codeoss.framework.view.parser;

import org.springframework.stereotype.Service;

import com.codeoss.framework.expression.Expression;
import com.codeoss.framework.expression.ExpressionUtil;
import com.codeoss.framework.util.StringUtils;
import com.codeoss.framework.view.component.Button;
import com.codeoss.framework.view.component.Component;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ObjectNode;

@Service
public class ButtonParser extends AbstractComponentParser {
	protected void setAttributes(IParseContext parseContext, ObjectNode objectNode, Component component) {
		super.setAttributes(parseContext, objectNode, component);
		Button button = (Button) component;
		String labelExpression = objectNode.get("labelExpression").asText();
		if (!StringUtils.isEmpty(labelExpression)) {
			Expression expression = ExpressionUtil.convertLabelToExpression(labelExpression,Expression.EXPRESSION_TYPE_JAVASCRIPT);
			button.setLabelExpression(expression);
		}
		ObjectNode propertyMapNode=(ObjectNode)objectNode.get("propertyMap");
		if(propertyMapNode!=null) {
			JsonNode jsonNode=propertyMapNode.get("colorType");
			if(jsonNode!=null) {
				String colorType=propertyMapNode.get("colorType").asText();
				button.setColorType(colorType);
			}
			
			jsonNode = propertyMapNode.get("confirmMessage");
			if (jsonNode != null) {
				String confirmMessage = propertyMapNode.get("confirmMessage").asText();
				button.setConfirmMessage(confirmMessage);
			}
			
			jsonNode = propertyMapNode.get("clientPageCode");
			if (jsonNode != null) {
				String clientPageCode = propertyMapNode.get("clientPageCode").asText();
				button.setClientPageCode(clientPageCode);
			}
			
			jsonNode = propertyMapNode.get("readonly");
			if (jsonNode != null) {
				String readonlyString=jsonNode.asText();
				if (!StringUtils.isEmpty(readonlyString)) {
					Expression expression = ExpressionUtil.convertToExpression(readonlyString,Expression.EXPRESSION_TYPE_JAVASCRIPT);
					button.setReadOnlyExpression(expression);
				}
			}
			
			jsonNode = propertyMapNode.get("serverPageCode");
			if (jsonNode != null) {
				String serverPageCode = propertyMapNode.get("serverPageCode").asText();
				button.setServerPageCode(serverPageCode);
			}
			
			jsonNode = propertyMapNode.get("block");
			if (jsonNode != null) {
				button.setBlock(jsonNode.asBoolean());
			}
			
			jsonNode = propertyMapNode.get("para1");
			if (jsonNode != null) {
				button.setPara1(jsonNode.asText());
			}
			
			jsonNode = propertyMapNode.get("para2");
			if (jsonNode != null) {
				button.setPara2(jsonNode.asText());
			}
		}
	}

	@Override
	protected Component createComponent(IParseContext parseContext, ObjectNode objectNode) {
		// TODO Auto-generated method stub
		return new Button();
	}

}
